$.ready(function () {
    $("#entry_date").datepicker({ dateFormat: "yy-mm-dd" });

    validate();
    autoComplete();
    autoComplete1();
    /*var a= 'aaa';
     var b = '上海';
     getStringLen(a);
     console.log('aaaaaaa',getStringLen(a));
     console.log('aaaaaaa',getStringLen(b));*/
}());

function validate(){
    $('#form1').validate({
        errorPlacement:function (error, element) {
            element.parent().append(error);
        }
    });
}

function autoComplete(){
    $("#ipt_name").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.name,
                            value:  item.user_name,
                            job_name: item.position_name,
                            user_id:  item.user_id,
                            job_id :item.job_id,
                            city_id :item.city_id,
                            user_code:item.user_code,
                            department_name:item.department_name,
                            m_level:item.m_level,
                            p_level:item.p_level,
                            date:item.enter_date
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var items = ui.item;

            var user_id = items.user_id,
                input1 =$('input[type="hidden"]')[0];

            input1.value = user_id;
            $('#work_city_id').val(items.city_id);
            $('#job_id').val(items.job_id);
            var user_code = items.user_code,        //工号
                job_name = items.job_name,          //职位
                p_level = items.p_level,            //职级p
                m_level = items.m_level,            //职级m
                dep_name = items.department_name,   //部门
                date = items.date;                  //入职时间

            var ipt1 = $('#ipt_code'),
                ipt2 = $('#ipt_job'),
                ipt3 = $('#ipt_level'),
                ipt4 = $('#ipt_department'),
                ipt5 = $('#ipt_entry');

            ipt1.val(user_code);
            ipt2.val(job_name);
            ipt3.val(p_level + m_level);
            ipt4.val(dep_name);
            ipt5.val(date);

            positionResult = true;
        }
    });

    $("#ipt_name").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });

    $("#ipt_name").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(".leader_job").val("");
            $(this).val("");

        }
    });

    jobResult = false;
    positionResult = false;
}

function autoComplete1(){
    $("#ipt_code").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.code_name,
                            value:  item.user_code,
                            job_name: item.position_name,
                            user_id:  item.user_id,
                            job_id :item.job_id,
                            city_id :item.city_id,
                            user_name:item.user_name,
                            department_name:item.department_name,
                            m_level:item.m_level,
                            p_level:item.p_level,
                            date:item.enter_date
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var items = ui.item;

            var user_id = items.user_id,
                //input1 = $(this).parent().find('input[type="hidden"]')[0];
                input1 =$('input[type="hidden"]')[0];

            input1.value = user_id;
            $('#work_city_id').val(items.city_id);
            $('#job_id').val(items.job_id);
            var user_name = items.user_name,        //姓名
                job_name = items.job_name,          //职位
                p_level = items.p_level,            //职级p
                m_level = items.m_level,            //职级m
                dep_name = items.department_name,   //部门
                date = items.date;                  //入职时间

            var ipt0 = $('#ipt_name'),
                ipt2 = $('#ipt_job'),
                ipt3 = $('#ipt_level'),
                ipt4 = $('#ipt_department'),
                ipt5 = $('#ipt_entry');

            ipt0.val(user_name);
            ipt2.val(job_name);
            ipt3.val(p_level + m_level);
            ipt4.val(dep_name);
            ipt5.val(date);

            positionResult = true;
        }
    });

    $("#ipt_code").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });
    $("#ipt_code").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(".leader_job").val("");
            $(this).val("");
        }
    });

    jobResult = false;
    positionResult = false;
}
//判断输入2个字再开始搜索
function getStringLen(B){
    var A=B.match(/[^\x00-\xff]/ig);
    return B.length+(A==null?0:A.length)
    /*aMatch = B.match(/[^\x00-\x80]/g);
    return (B.length + (! aMatch ? 0 : aMatch.length));*/
}
